package converters

import (
	protos "goim-pro/api/protos/salty"
	"goim-pro/internal/app/constants"
	"goim-pro/internal/app/models"
	"goim-pro/pkg/utils"
)

// ConvertEntities2ProtoForGroups - convert group list from model schema to proto profile
func ConvertEntities2ProtoForGroups(groups []models.Group) (pbGroups []*protos.GroupProfile) {
	pbGroups = make([]*protos.GroupProfile, len(groups))
	for i, group := range groups {
		pbGroups[i] = ConvertEntity2ProtoForGroupProfile(group)
	}
	return pbGroups
}

// ConvertEntity2ProtoForGroupProfile - convert group from models schema to proto schema
func ConvertEntity2ProtoForGroupProfile(groupProfile models.Group) *protos.GroupProfile {
	return &protos.GroupProfile{
		GroupId:     groupProfile.GroupId,
		GroupNumber: groupProfile.GroupNumber,
		GroupName:   groupProfile.Name,
		Avatar:      groupProfile.Avatar,
		Notice:      groupProfile.Notice,
		CreateTime:  utils.ParseTimeToTimestamp(groupProfile.CreatedAt),
		UpdateTime:  utils.ParseTimeToTimestamp(groupProfile.UpdatedAt),
		Members:     convertEntity2ProtoForMemberProfiles(groupProfile.Members),
	}
}

func convertEntity2ProtoForMemberProfiles(members []models.Member) (protoMembers []*protos.GroupMemberProfile) {
	protoMembers = make([]*protos.GroupMemberProfile, len(members))
	for i, member := range members {
		protoMembers[i] = &protos.GroupMemberProfile{
			GroupId:    member.GroupId,
			Nickname:   member.Alias,
			Role:       consts.MemberRoleStringMapping[member.Role],
			JoinTime:   utils.ParseTimeToTimestamp(member.CreatedAt),
			UpdateTime: utils.ParseTimeToTimestamp(member.UpdatedAt),
			MemberUser: ConvertEntity2ProtoForUserProfile(member.User),
		}
	}
	return protoMembers
}
